<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Exception Handling
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial033.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial035.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc73">4.11</A>&nbsp;&nbsp;Exception Handling</H2>
It is sometimes necessary to exit prematurely from an executing
procedure, for example because some situation was detected which
makes continuing impossible.
In this situation, one wants to return to some defined state and
perform some kind of recovery action.
This functionality is provided by block/3 and exit_block/1.

	<BLOCKQUOTE CLASS="figure"><DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV>
	<DIV CLASS="center">
	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#DB9370">
	
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>block(Goal, BTag, Recovery)</B><DD CLASS="dd-description"><BR>
like <TT>call(Goal)</TT>, except that in addition a Recovery goal is set up, which
 can be called by <TT>exit_block</TT> from anywhere inside the
 call to Goal. When <TT>exit_block(ETag)</TT> is called, then if <TT>ETag</TT> 
 unifies with a <TT>BTag</TT> from an enclosing <TT>block</TT>, the
 recovery goal associated with that <TT>block</TT> is called, with the system
 immediately failing back to where the <TT>block</TT> was called. In
 addition, <TT>ETag</TT> can be used to pass information to the recovery 
 goal, if <TT>BTag</TT> occurs as an argument of <TT>Recovery</TT>. 
<DT CLASS="dt-description"><B>exit_block(ETag)</B><DD CLASS="dd-description"><BR>
will transfer control to the innermost enclosing block/3 whose
 <TT>BTag</TT> argument unifies with <TT>ETag</TT>.
</DL>

	</TD>
</TR></TABLE>
	</DIV>
	<BR>
<BR>
<DIV CLASS="center">Figure 4.6: Exception Handling</DIV><BR>
<BR>

	<DIV CLASS="center"><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
By wrapping a predicate call into block/3, any irregular termination
can be caught and handled, e.g.

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
protected_main(X,Y,Z) :-
    block(
        main(X,Y,Z),
        Problem,
        printf("Execution of main/3 aborted with %w%n", [Problem])
    ).

main(X,Y,Z) :-
    ...,
    ( test(...) -&gt; ... ; exit_block(test_failed) ),
    ...,
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
When built-in predicates raise errors, this results in the predicate
being exited with the tag <CODE>abort</CODE>, which can also be caught:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
?- block(X is 1//0, T, true).
arithmetic exception in //(1, 0, X)
X = X
T = abort
Yes (0.00s cpu)
</PRE></BLOCKQUOTE>
Note that timeouts and stack overflows also lead to exits and can be
caught this way.<BR>
<BR>
<HR>
<A HREF="tutorial033.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial023.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial035.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
